package org.moonlight.jvm.instructions.references.array;

import org.moonlight.jvm.instructions.base.NoOperandsInstruction;
import org.moonlight.jvm.rtda.exclusive.Frame;
import org.moonlight.jvm.rtda.exclusive.OperandStack;
import org.moonlight.jvm.rtda.share.Object;

/**
 * array_length指令: 用于获取数组长度. 只需要一个操作数，即从操作数栈顶弹出的数组引用
 *
 * @author Moonlight
 * @createTime 2023/9/15 14:07
 **/
public class ArrayLength extends NoOperandsInstruction {

    @Override
    public void execute(Frame frame) {
        OperandStack stack = frame.getOperandStack();
        Object arrRef = stack.popRef();
        if (arrRef == null) {
            throw new NullPointerException();
        }
        int arrayLength = arrRef.arrayLength();
        stack.pushInt(arrayLength);
    }

}
